package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Permission;
import cn.wolfcode.crm.mapper.PermissionMapper;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPermissionService;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService{
    @Autowired
    PermissionMapper mapper;
    @Autowired
    private RequestMappingHandlerMapping rmhm;

    public void deleteByPrimaryKey(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    public void insert(Permission record) {
        mapper.insert(record);
    }

    public Permission selectByPrimaryKey(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<Permission> selectAll() {
        return mapper.selectAll();
    }

    public void updateByPrimaryKey(Permission record) {
        mapper.updateByPrimaryKey(record);
    }

    public PageResult query(QueryObject qo) {
        int total = mapper.queryForCount(qo);
        if (total == 0) {
            return new PageResult();
        }
        List<Permission> rows = mapper.queryForList(qo);
        return new PageResult(rows, total);
    }

    public void reload() {
        //去重
        List<Permission> list = mapper.selectAll();
        Set<String> set = new HashSet<>();
        //快捷foreach : iter
        for (Permission permission : list) {
            set.add(permission.getResource());
        }
        //1.获取映射方法集合对象(贴有requestMapper这个注解集合)
        Map<RequestMappingInfo, HandlerMethod> handlerMethods = rmhm.getHandlerMethods();
        Collection<HandlerMethod> values = handlerMethods.values();
        //2.遍历方法集合, 判断方法是否有权限注解
        RequiresPermissions anno = null;
        Permission permission = null;
        for (HandlerMethod method : values) {
             anno = method.getMethodAnnotation(RequiresPermissions.class);
             if(anno != null){
                 //RequiresPermissions value是一个数组,只需要第一个
                 String resource = anno.value()[0];
                 if(set.contains(resource)){
                     continue;
                 }
                 String name = method.getMethodAnnotation(PermissionName.class).value();
                 permission = new Permission();
                 permission.setName(name);
                 permission.setResource(resource);
                 this.insert(permission);
             }
        }
    }

    public List<Permission> queryPermissionByRoleId(Long roleId) {
        return mapper.queryPermissionByRoleId(roleId);
    }
}
